Skip to main content

string

string is a type used for textual data such as 'Hello World!'

Let's start with how to create a string:

warning

Avoid using String() as a constructor for strings

Here are some more advanced methods for string operations:

tip

Try to think about Strings as 1-D Arrays. For example the String "lailai" is kind of an Array:

['l', 'a', 'i', 'l', 'a', 'i'].

Let's go over some methods and properties frequently used:

  • toUpperCase() / toLowerCase(): changes strings case type

  • \{special character} - escapes it. Hence views it as a literal

  • String.length is the only property, the rest are methods!

  • String.at(index) - returns the char at said index

  • String.includes(searchString) - boolean based on if searchString is in the String

  • String.indexOf(searchValue) - returns the index of the first instance of said value

Let's view an example of a practical scenario, using some string methods:

 

info

As one can imagine, there exists many, many more methods for Strings.

For more examples and details, visit: Strings - MDN for more information.